/**
 * to风格封装方式
 * @param promise 传入的需要使用的promise对象
 * @returns 返回[err,res] promise
 */
function toResult<E = unknown, R = unknown>(promise: Promise<unknown>): Promise<[E, R] | any> {
  if (!promise || Object.prototype.toString.call(promise) !== '[object Promise]') {
    return new Promise((resolve, reject) => {
      reject(new Error('requires promises as the param'));
    }).catch((err) => [err, null]);
  }
  return promise.then((...args) => [null, ...args]).catch((err) => [err, null]);
}

export { toResult };
